package com.dingding.service.account.core.special.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.beans.special.SpecialDepositAccount;
import com.dingding.common.core.beans.special.Specialdepositrecord;
import com.dingding.common.core.beans.special.Specialuseraccount;
import com.dingding.common.core.beans.special.Specialuserlogin;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facade.account.common.dto.special.GetSpecialDepositAccountDTO;
import com.dingding.facade.account.common.dto.special.ReqAddSpecialDepositAccount;
import com.dingding.facade.account.common.dto.special.ReqDeleteSpecialDepositAccount;
import com.dingding.facade.account.common.dto.special.ReqGetSpecialDepositAccount;
import com.dingding.facade.account.common.dto.special.ReqSUAStatus;
import com.dingding.facade.account.common.dto.special.ReqSpecialWithdrawals;
import com.dingding.facade.account.common.dto.special.ResGetSpecialDepositAccount;
import com.dingding.facade.account.common.dto.special.ResSUAStatus;
import com.dingding.facade.account.common.dto.special.SpecialWithdrawalsDTO;
import com.dingding.facade.user.facade.special.driver.ISpecialUserInfoFacade;
import com.dingding.service.account.core.special.dao.ISpecialUserAccountDao;
import com.dingding.service.account.core.special.service.ISpecialUserAccountService;

/**
 * 
 * @ClassName: SpecialUserAccountServiceImpl 
 * @Description: 专车用户账户业务逻辑实现类
 * @author YHQ
 * @date 2016年12月21日 上午10:55:41
 * @since V2.5.8
 * 
 */
@Component("specialUserAccountService")
public class SpecialUserAccountServiceImpl implements ISpecialUserAccountService{

	@Resource(name = "specialUserAccountDao")
	private ISpecialUserAccountDao specialUserAccountDao;
	
	@Resource(name = "specialUserAccountFacade_special")
	private ISpecialUserInfoFacade specialUserInfoFacade;
	
	/**
	 * 
	 * @Title: getSpecialUserAuthenticationStatus 
	 * @Description: 获取专车用户提现认证状态 
	 * @param map 客户端请求参数
	 * @author YHQ 
	 * @since V2.5.8
	 * @return ResSUAStatus    返回类型
	 * 
	 */
	@Override
	public ResponseEntity<Object> getSpecialUserAuthenticationStatus(ReqSUAStatus reqSUAStatus) {
		
		ResponseEntity<Object> responseEntity = new ResponseEntity<Object>();
		
		// 客户端请求Bean是否为空
		if(reqSUAStatus == null || !ToolUtil.validateParam(reqSUAStatus)){
			responseEntity.setCode(HttpStatus.SC_BAD_REQUEST);
			responseEntity.setErrorMessage("获取专车用户提现认证状态客户端请求参数为空，请注意查收！");
			responseEntity.setPromptMessage("请求数据异常！");
			return null;
		}
		
		// 获取专车用户提现认证状态
		ResSUAStatus resSUAStatus = specialUserAccountDao.getSpecialUserAuthenticationStatus(reqSUAStatus.getUserId());
		
		if(resSUAStatus == null || resSUAStatus.getIsDeposit() == null){
			
			resSUAStatus = new ResSUAStatus();
			// 设置未体现状态
			resSUAStatus.setIsDeposit(1);
		}
		
		responseEntity.setCode(HttpStatus.SC_OK);
		responseEntity.setResponseBody(resSUAStatus);
		
		return responseEntity;
	}

	/**
	 * 
	 * @Title: addSpecialDepositAccount 
	 * @Description: 添加专车用户提现账户
	 * @param reqAddSpecialDepositAccount 客户端请求参数
	 * @author YHQ 
	 * @since V2.6.3
	 * @return ResponseEntity<Object>    返回类型
	 * 
	 */
	@Override
	public ResponseEntity<Object> addSpecialDepositAccount(ReqAddSpecialDepositAccount reqAddSpecialDepositAccount) {
		
		ResponseEntity<Object> responseEntity = new ResponseEntity<Object>();
		
		// 判断客户端请求参数是否为空
		if(reqAddSpecialDepositAccount == null || !ToolUtil.validateParam(reqAddSpecialDepositAccount)){
			
			responseEntity.setCode(HttpStatus.SC_BAD_REQUEST);
			responseEntity.setErrorMessage("客户端请求参数为空");
			responseEntity.setPromptMessage("请求参数异常");
			
			return responseEntity;
		}
		
		// 创建提现账户对象
		SpecialDepositAccount depositAccount = new SpecialDepositAccount();
		
		if(!validateSpecialAddAccount(reqAddSpecialDepositAccount, depositAccount, responseEntity)){
			return responseEntity;
		}
		
		if(depositAccount.getDepositAccountId() == null){
			
			// 提现账户
			depositAccount.setDepositAccount(reqAddSpecialDepositAccount.getAlipayAccount());
			// 提现账户姓名
			depositAccount.setDepositAccountName(reqAddSpecialDepositAccount.getAlipayName());
			// 用户id
			depositAccount.setSpecialUserId(reqAddSpecialDepositAccount.getUserId());
			// 创建时间
			depositAccount.setCreateTime(new Date());
			// 提现账户状态   1:启用   2:禁用
			depositAccount.setDepositAccountStatus(1);
		}else{
			// 提现账户状态   1:启用   2:禁用
			depositAccount.setDepositAccountStatus(1);
		}
		
		boolean flag = specialUserAccountDao.addSpecialDepositAccount(depositAccount);
		
		if(flag){
			responseEntity.setCode(HttpStatus.SC_OK);
		}else{
			responseEntity.setCode(HttpStatus.SC_BAD_REQUEST);
			responseEntity.setErrorMessage("添加专车用户提现账户失败");
			responseEntity.setPromptMessage("添加专车用户提现账户失败");
		}
		
		return responseEntity;
	}

	/**
	 * 
	 * @Title: validateSpecialAddAccount 
	 * @Description: 验证专车添加提现账户条件 
	 * @param reqAddSpecialDepositAccount 客户端请求参数
	 * @param depositAccount 专车提现账户对象
	 * @param responseEntity 响应Bean
	 * @author YHQ 
	 * @since V2.6.3
	 * @return boolean    返回类型
	 * 
	 */
	public boolean validateSpecialAddAccount(ReqAddSpecialDepositAccount reqAddSpecialDepositAccount,SpecialDepositAccount depositAccount,ResponseEntity<Object> response){
		
		// 获取专车提现账户列表
		List<SpecialDepositAccount> specialDepositAccounts = specialUserAccountDao.getSpecialDepositAccount(reqAddSpecialDepositAccount.getUserId());
		
		// 如果该用户的用户列表不为空，则需要判断账户是否超过五个，已经新添加的用户账户是否存在
		if(specialDepositAccounts != null && specialDepositAccounts.size() > 0){
			
			// 启用账户个数
			int number = 0;
			// 是否存在相同账户 true:存在   false:不存在
			boolean flag = false;
			
			// 遍历全部用户账户列表
			for (SpecialDepositAccount specialDepositAccount : specialDepositAccounts){
				
				// 统计启用个数
				if(specialDepositAccount.getDepositAccountStatus() == 1){
					number++;
				}
				
				// 判断用户列表中是否存在该提现账户
				if(specialDepositAccount.getDepositAccount().equals(reqAddSpecialDepositAccount.getAlipayAccount())){
					if(specialDepositAccount.getDepositAccountStatus() == 1){ // 启用状态
						flag = true;
					}else if(specialDepositAccount.getDepositAccountStatus() == 2){ // 禁用状态
						depositAccount = specialDepositAccount;
					}
				}
			}
			
			// 添加账户个数不能超过5个
			if(number > 4){
				response.setCode(HttpStatus.SC_BAD_REQUEST);
				response.setErrorMessage("您最多只能添加5个用户账户");
				response.setPromptMessage("您最多只能添加5个用户账户");
				return false;
			}
			
			// 该账户是否存在
			if(flag){
				response.setCode(HttpStatus.SC_BAD_REQUEST);
				response.setErrorMessage("该账户已存在");
				response.setPromptMessage("该账户已存在");
				return false;
			}
		}
		
		return true;
	}
	
	/**
	 * 
	 * @Title: deleteSpecialDepositAccount 
	 * @Description: 删除专车用户提现账户
	 * @param reqDeleteSpecialDepositAccount 客户端请求参数
	 * @author YHQ 
	 * @since V2.6.3
	 * @return ResponseEntity<Object>    返回类型
	 * 
	 */
	@Override
	public ResponseEntity<Object> deleteSpecialDepositAccount(ReqDeleteSpecialDepositAccount reqDeleteSpecialDepositAccount) {
		
		ResponseEntity<Object> responseEntity = new ResponseEntity<Object>();
		
		// 判断客户端请求参数是否为空
		if(reqDeleteSpecialDepositAccount == null || !ToolUtil.validateParam(reqDeleteSpecialDepositAccount)){
			
			responseEntity.setCode(HttpStatus.SC_BAD_REQUEST);
			responseEntity.setErrorMessage("客户端请求数据为空");
			responseEntity.setPromptMessage("请求参数异常");
			
			return responseEntity;
		}
		
		// 删除专车用户提现账户
		boolean flag = specialUserAccountDao.deleteSpecialDepositAccount(reqDeleteSpecialDepositAccount.getDepositAccountId(),reqDeleteSpecialDepositAccount.getUserId());
		
		if(flag){
			responseEntity.setCode(HttpStatus.SC_OK);
		}else{
			responseEntity.setCode(HttpStatus.SC_BAD_REQUEST);
			responseEntity.setErrorMessage("删除专车用户提现账户失败");
			responseEntity.setPromptMessage("删除专车用户提现账户失败");
		}
		
		return responseEntity;
	}

	/**
	 * 
	 * @Title: getSpecialDepositAccountList 
	 * @Description: 获取专车用户提现账户列表
	 * @param reqGetSpecialDepositAccount 
	 * @author YHQ 
	 * @since V2.6.3
	 * @return ResponseEntity<Object>    返回类型
	 * 
	 */
	@Override
	public ResponseEntity<Object> getSpecialDepositAccountList(
			ReqGetSpecialDepositAccount reqGetSpecialDepositAccount) {
		
		ResponseEntity<Object> responseEntity = new ResponseEntity<Object>();
		
		// 判断客户端请求参数是否为空
		if(reqGetSpecialDepositAccount == null || !ToolUtil.validateParam(reqGetSpecialDepositAccount)){
			return null;
		}
		
		// 响应Bean
		GetSpecialDepositAccountDTO getSpecialDepositAccountDTO = new GetSpecialDepositAccountDTO();
		
		// 获取专车用户提现账户列表
		List<ResGetSpecialDepositAccount> resGetSpecialDepositAccounts = specialUserAccountDao.getSpecialDepositAccountList(reqGetSpecialDepositAccount.getUserId());
		
		getSpecialDepositAccountDTO.setAccountList(resGetSpecialDepositAccounts);
		
		responseEntity.setCode(HttpStatus.SC_OK);
		responseEntity.setResponseBody(getSpecialDepositAccountDTO);
		
		return responseEntity;
	}

	/**
	 * 
	 * @Title: updateSpecialUserAccount 
	 * @Description: 修改专车用户账户信息
	 * @param specialuseraccount 用户账户信息
	 * @author YHQ 
	 * @since V2.6.3
	 * @return boolean    返回类型
	 * 
	 */
	@Override
	public boolean updateSpecialUserAccount(Specialuseraccount specialuseraccount) {
		
		// 判断客户端请求参数是否为空
		if(specialuseraccount == null || !ToolUtil.validateParam(specialuseraccount)){
			return false;
		}
		
		return specialUserAccountDao.updateSpecialUserAccount(specialuseraccount);
	}

	/**
	 * 
	 * @Title: specialWithdrawals 
	 * @Description: 用户提现(专车端)
	 * @param reqSpecialWithdrawals 用户提现(专车端)客户端请求Bean
	 * @author YHQ 
	 * @since V2.6.3
	 * @return ResponseEntity<Object>    返回类型
	 * 
	 */
	@Override
	public ResponseEntity<Object> specialWithdrawals(ReqSpecialWithdrawals reqSpecialWithdrawals) {
		
		ResponseEntity<Object> responseEntity = new ResponseEntity<Object>();
		
		// 判断客户端请求参数是否为空
		if(reqSpecialWithdrawals == null || !ToolUtil.validateParam(reqSpecialWithdrawals)){
			
			responseEntity.setCode(HttpStatus.SC_BAD_REQUEST);
			responseEntity.setErrorMessage("客户端请求参数为空");
			responseEntity.setPromptMessage("请求参数异常");
			
			return responseEntity;
		}
		
		// 获取专车用户登录信息
		Specialuserlogin specialuserlogin = specialUserInfoFacade.getSpecialUserLogin(reqSpecialWithdrawals.getUserId());
		
		// 获取专车司机的账户信息
		Specialuseraccount specialuseraccount = specialUserAccountDao.getSpecialUserAccount(reqSpecialWithdrawals.getUserId());
		
		// 用户账户信息不存在
		if(specialuserlogin == null || specialuseraccount == null){
			
			responseEntity.setCode(HttpStatus.SC_BAD_REQUEST);
			responseEntity.setErrorMessage("用户不存在");
			responseEntity.setPromptMessage("用户不存在");
			return responseEntity;
		}
		
		SpecialWithdrawalsDTO specialWithdrawalsDTO = new SpecialWithdrawalsDTO();
		
		// 是否设置提现密码   1:未提现  2:提现
		if(specialuserlogin.getIsDeposit() != 2){
			
			responseEntity.setCode(HttpStatus.SC_BAD_REQUEST);
			responseEntity.setErrorMessage("该用户未设置提现密码");
			responseEntity.setPromptMessage("该用户未设置提现密码");
			
			return responseEntity;
		}
		
		// 判断当日是否还能提现
		if(specialuserlogin.getDepositNumber() > 0){
			
			responseEntity.setCode(HttpStatus.SC_BAD_REQUEST);
			responseEntity.setErrorMessage("本日提现次数已使用");
			responseEntity.setPromptMessage("本日提现次数已使用");
			
			return responseEntity;
		}
		
		// 判断当日是否还能提现
		if(reqSpecialWithdrawals.getDepositMoney().compareTo(new BigDecimal(1)) < 0){
			
			responseEntity.setCode(HttpStatus.SC_BAD_REQUEST);
			responseEntity.setErrorMessage("提现金额不能小于1块钱");
			responseEntity.setPromptMessage("提现金额不能小于1块钱");
			
			return responseEntity;
		}
		
		// 判断是否余额是否充足
		if(specialuseraccount.getCurrencyAmount().compareTo((reqSpecialWithdrawals.getDepositMoney().add(specialuseraccount.getFreezeCashAmount()))) < 0){
			
			responseEntity.setCode(HttpStatus.SC_OK);
			specialWithdrawalsDTO.setDepositInfo("余额不足");
			specialWithdrawalsDTO.setDepositStatus(2);
			responseEntity.setResponseBody(specialWithdrawalsDTO);
			
			return responseEntity;
		}
		
		// 判断提现密码是否正确
		if(!specialuserlogin.getDepositPwd().equals(reqSpecialWithdrawals.getDepositPwd())){
			
			responseEntity.setCode(HttpStatus.SC_OK);
			specialWithdrawalsDTO.setDepositInfo("提现密码错误");
			specialWithdrawalsDTO.setDepositStatus(1);
			responseEntity.setResponseBody(specialWithdrawalsDTO);
			
			return responseEntity;
		}
		
		// 添加一条提现明细
		this.addDepositRecord(reqSpecialWithdrawals);
		
		// 提现冻结金额
		this.updateFreezeCashAmount(specialuseraccount, reqSpecialWithdrawals);
		
		// 设置本日提现次数
		this.updateDepositNumber(specialuserlogin);
		
		responseEntity.setCode(HttpStatus.SC_OK);
		specialWithdrawalsDTO.setDepositInfo("提现成功");
		specialWithdrawalsDTO.setDepositStatus(3);
		responseEntity.setResponseBody(specialWithdrawalsDTO);
		
		return responseEntity;
	}

	/**
	 * 
	 * @Title: addDepositRecord 
	 * @Description: 添加一条提现记录
	 * @param reqUserDepositDTO 提现请求参数
	 * @author YHQ 
	 * @since V2.4.0 
	 * @return boolean    返回类型
	 * 
	 */
	public boolean addDepositRecord(ReqSpecialWithdrawals reqSpecialWithdrawals){
		
		// 创建提现对象
		Specialdepositrecord specialdepositrecord = new Specialdepositrecord();
		
		// 提现用户Id
		specialdepositrecord.setUserId(reqSpecialWithdrawals.getUserId());
		// 提现金额
		specialdepositrecord.setDepositMoney(reqSpecialWithdrawals.getDepositMoney());
		// 支付宝账号
		specialdepositrecord.setZfbAccount(reqSpecialWithdrawals.getZfbAccount());
		// 支付宝名称
		specialdepositrecord.setZfbName(reqSpecialWithdrawals.getZfbName());
		
		// 提现记录主键
		specialdepositrecord.setSddId(ToolUtil.getUid());
		// 提现时间
		specialdepositrecord.setDepositTime(new Date());
		// 提现状态
		specialdepositrecord.setDepositStatus(1);
		
		return specialUserAccountDao.addSpecialDepositRecord(specialdepositrecord);
	}

	/**
	 * 
	 * @Title: updateFreezeCashAmount 
	 * @Description: 提现后的专车用户账户信息（冻结金额）
	 * @param specialuseraccount 用户账户信息
	 * @param reqSpecialWithdrawals 客户端请求参数
	 * @author YHQ 
	 * @since V2.6.3
	 * @return boolean    返回类型
	 * 
	 */
	public boolean updateFreezeCashAmount(Specialuseraccount specialuseraccount,ReqSpecialWithdrawals reqSpecialWithdrawals){
		
		// 冻结金额
		specialuseraccount.setFreezeCashAmount(specialuseraccount.getFreezeCashAmount().add(reqSpecialWithdrawals.getDepositMoney()));
		
		return specialUserAccountDao.updateSpecialUserAccount(specialuseraccount);
	}
	
	/**
	 * 
	 * @Title: updateDepositNumber 
	 * @Description: 修改提现次数
	 * @param @return    设定文件 
	 * @author YHQ 
	 * @since V2.6.3
	 * @return boolean    返回类型
	 * 
	 */
	public boolean updateDepositNumber(Specialuserlogin specialuserlogin){
		
		// 提现次数加一
		specialuserlogin.setDepositNumber(specialuserlogin.getDepositNumber() + 1);
		
		return specialUserInfoFacade.updateSpecialUserLogin(specialuserlogin);
	}
	
}
